<?php

class Retorno implements IRetorno {
    private $StMessage;
    private $ItLine;
    private $StFile;
    private $Type;
    const ERROR_RETURN = 0;
    const SUCCESS_RETURN = 1;
    const DEFAULT_RETURNPACKAGE_SESSION = SESSION_INTERFACE;
    const DEFAULT_RETURNINDEX_SESSION = 'RETORNO';

    public function __construct($StMessage, $ItType = 0, $StFile = __FILE__, $ItLine = false) {
        $this->StMessage = $StMessage;
        $this->ItLine = $ItLine;
        $this->StFile = $StFile;
        $this->Type = $ItType;

        if( $this->Type != self::ERROR_RETURN && $this->Type != self::SUCCESS_RETURN) {
            return false;
        }

        $this->saveReturn();

    }
    /**
     * @method  saveReturn
     *
     * Salva o retorno na sessao
     *
     * @since   2011-04-17
     * @author  Richard Brandao <richardsbrandao@gmail.com>
     */
    private function saveReturn() {
        Dracosys::getSession()->setValue(self::DEFAULT_RETURNPACKAGE_SESSION, self::DEFAULT_RETURNINDEX_SESSION, $this);
    }

    public function getMessage() {
        return $this->StMessage;
    }
    public function getLine(){
        return $this->ItLine;
    }
    public function getFile(){
        return $this->StFile;
    }
    public function getType() {
        return $this->Type;
    }

    public function __toString() {
        $string = 'Mensagem: <span class="return-message">' . $this->StMessage . '</span>';
        $string .= !empty($this->ItLine) ? ' - Linha: <span class="return-message">' . $this->ItLine . '</span> ' : '';
        $string .= !empty($this->StFile) ? ' - Arquivo: <span class="return-message">' . $this->StFile . '</span>' : '';
        return $string;
    }
    public function getReturnType() {
        if( $this->Type == self::ERROR_RETURN ) {
            return 'ERROR';
        } else if( $this->Type == self::SUCCESS_RETURN ) {
            return 'SUCCESS';
        } else {
            return 'UNKNOW';
        }
    }
    public function parseArray() {
        return array(
            'StMessage' => $this->StMessage,
            'ItLine' => $this->ItLine,
            'StFile' => $this->StFile,
            'Type' => ($this->Type == self::ERROR_RETURN) ? 'error' : 'success',
            'toString' => $this->__toString()
        );
    }

}


?>
